"""
插入排序 默认前n个数据有序 第n个保存在key中 依次往前查找小于的第一个数字 赋值
"""
import random


def insertSort(nums):
    if not nums:
        return nums
    for i in range(1, len(nums)):
        key = nums[i]
        j = i - 1
        while j >= 0 and nums[j] > key:
            nums[j + 1] = nums[j]
            j -= 1
        nums[j+1] = key
    return nums


if __name__ == '__main__':
    ls = [random.randint(1, 100) for _ in range(10)]
    print(ls)
    print(sorted(ls))
    print(insertSort(ls))

